package com.qq.googleplay.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.qq.googleplay.utils.ToastUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * ============================================================
 * 版 权 ： Google互联网有限公司版权所有 (c) 2016
 * 作 者 : 陈冠杰
 * 版 本 ： 1.0
 * 创建日期 ：2016/3/20 17:01
 * 描 述 ：抽取BaseActivity   管理所有activity 方便退出
 * 修订历史 ：
 * ============================================================
 **/
public class BaseActivity extends AppCompatActivity {
    //管理所有运行的BaseActivity
   public final  static List<BaseActivity> mActivities = new LinkedList<>();

    //	private KillAllReceiver receiver;
    //	private class KillAllReceiver extends BroadcastReceiver{
    //
    //		@Override
    //		public void onReceive(Context context, Intent intent) {
    //			finish();
    //		}
    //	}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //		receiver=new KillAllReceiver();
        //		IntentFilter filter=new IntentFilter("com.itheima.google.killall");
        //		registerReceiver(receiver, filter);

        synchronized (mActivities){
            mActivities.add(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //		if(receiver!=null){
        //			unregisterReceiver(receiver);
        //			receiver=null;
        //		}

        synchronized (mActivities){
            mActivities.remove(this);
        }
    }

    public void killAll(){

        //复制一份mActivity集合
        List<BaseActivity> copy ;
        synchronized (mActivities){
            copy = new LinkedList<BaseActivity>(mActivities);
        }

        for (BaseActivity activity : copy) {
            activity.finish();
        }

        android.os.Process.killProcess(android.os.Process.myPid());//杀死当前的进程
    }

    public void toast(String content){
        ToastUtils.toast(content);
    }

    public void toast(Activity activity, String content, int duration){
        ToastUtils.toast(activity,content,duration);
    }
}
